using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.SaveData;
using MainGame.GameSound;

namespace MainGame.Menu
{

  public interface IStartMenu
  {
    event MenuEvent OnPlayClicked;
    event MenuEvent OnExitClicked;
    event MenuEvent OnProfile;
    event MenuEvent OnContinue;
    event MenuEvent OnNewProfile;   
  }


  public class StartMenu : BaseMenu, IStartMenu
  {

    Button playBtn, exitBtn, continueBtn, StringBtn;
    string welcomeMsg, actionMsg;
    int counter = 4;
    SpriteFont smallFont, largeFont;
    Rectangle profileRect;
    SpriteBatch spriteBatch;
    


    public StartMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      spriteBatch = game.GetContext().SpriteBatch;
      bounds = game.GraphicsDevice.Viewport.Bounds;
      game.Services.AddService(typeof(IStartMenu), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(IStartMenu));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
      // Fonts
      smallFont = Game.Content.Load<SpriteFont>("small_font");
      largeFont = Game.Content.Load<SpriteFont>("large_font");
    
      
     
      // Play buttons
      playBtn = new Button(Game, @"Images//Menu//button2", new Rectangle(Game.GraphicsDevice.Viewport.Bounds.X + 625, 400, 120, 65), Color.Blue, "small_font", Locale.Strings.NewGame.Replace("\\n", "\n"), Color.White);
      AddButton(playBtn, () =>
      {
        playBtn.touched = true;
      });

      exitBtn = new Button(Game, @"Images//Menu//button2", new Rectangle(Game.GraphicsDevice.Viewport.Bounds.X + 70, 400, 120, 65), Color.Red, "large_font", Locale.Strings.Exit, Color.White);
      AddButton(exitBtn, () =>
      {
        exitBtn.touched = true;
      });

      
      // Profile rectangle
      profileRect = new Rectangle(410, 150, 350, 120); 
      
      Color color = Color.Black;
      color.A = 0;

      Color color2 = Color.Green;
      color2.A = 255;

      int profilesQuantity = Game.GetProfileList().Count;
      if (Game.GetContext().currentProfile.id == 0)
        welcomeMsg = String.Format(Locale.Strings.Hello, "");
      else
        welcomeMsg = String.Format(Locale.Strings.Hello, " " + Game.GetContext().currentProfile.username);

      actionMsg = Locale.Strings.CreateProfile;
      if (profilesQuantity != 0)
      {
        if (Game.GetContext().currentProfile.id == 0)
          actionMsg = Locale.Strings.ChooseProfile;
        else
          actionMsg = Locale.Strings.ChangeProfile;
      }

      StringBtn = new Button(Game, @"white_pixel", new Rectangle((int)(profileRect.X + ((profileRect.Width - smallFont.MeasureString(actionMsg).X) / 2)), profileRect.Y + 50, (int)smallFont.MeasureString(actionMsg).X, 45), color, "small_font", actionMsg, Color.Black);
      AddButton(StringBtn, () =>
      {
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);

        if (profilesQuantity != 0)
        {
          if (OnProfile != null)
            OnProfile();
        }
        else
        {
          if (OnNewProfile != null)
            OnNewProfile();
        }
      });

      continueBtn = new Button(Game, @"Images//Menu//Button2", new Rectangle(410, 400, 120, 65), Color.Green, "small_font", Locale.Strings.ContinueGame.Replace("\\n", "\n"), Color.White);
      if (Game.GetContext().currentProfile.HasSavedGames())
      {
        AddButton(continueBtn, () =>
        {
          continueBtn.touched = true;

        });
      }

      base.Initialize();
    }


    public override void Update(GameTime gameTime)
    {

      if (playBtn.touched)
      {
        counter--;
        if (counter < 0)
        {
          if ((OnPlayClicked != null))
            OnPlayClicked();
        }
      }

      if (exitBtn.touched)
      {
        counter--;
        if (counter < 0)
        {
          if ((OnExitClicked != null))
            OnExitClicked();
        }
      }

      if (continueBtn.touched)
      {
        counter--;
        if (counter < 0)
        {
          if ((OnContinue != null))
            OnContinue();
        }
      }
      // Input: back button pressed
      if (((Game1)Game).IsBackButtonPressed())
      {
        if (OnExitClicked != null)
          OnExitClicked();
      }

      base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);

      spriteBatch.Begin();
      spriteBatch.DrawString(largeFont, welcomeMsg, new Vector2(profileRect.X + ((profileRect.Width - largeFont.MeasureString(welcomeMsg).X) / 2) + 20, profileRect.Y + 10), Color.Black, 0, Vector2.Zero, 0.8f, SpriteEffects.None, 0);
      spriteBatch.DrawString(smallFont, actionMsg, new Vector2(StringBtn.bounds.Left - 2, StringBtn.bounds.Top + 3), new Color(230, 10, 0), 0, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
      spriteBatch.End();
    }

    public event MenuEvent OnPlayClicked;
    public event MenuEvent OnExitClicked;
    public event MenuEvent OnProfile;
    public event MenuEvent OnContinue;
    public event MenuEvent OnNewProfile;
    
 
  }
}
